﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Fin;
using nERP.Model;
using nERP.Model.Bas;

namespace nERP.ViewModel.Fin.OtherOutlayVMs
{
    public partial class OtherOutlayListVM : BasePagedListVM<OtherOutlay_View, OtherOutlaySearcher>
    {

        protected override IEnumerable<IGridColumn<OtherOutlay_View>> InitGridHeader()
        {
            return new List<GridColumn<OtherOutlay_View>>{

                this.MakeGridHeader(x => x.OtherOutlay_OtherOutlayCode).SetTitle(@Localizer["Page.其他支出单号"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_Supplier).SetTitle(@Localizer["Page.对方单位"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_MakeUser).SetTitle(@Localizer["Page.制单人"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_InvoicesDate).SetTitle(@Localizer["Page.单据日期"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_SettlementAccount).SetTitle(@Localizer["Page.结算账户"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_OutlayType).SetTitle(@Localizer["Page.支出类别"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_TotalPrice).SetTitle(@Localizer["Page.金额"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_Status).SetTitle(@Localizer["Page.单据状态"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),
                this.MakeGridHeader(x => x.OtherOutlay_IsValid).SetTitle(@Localizer["_Admin.IsValid"].Value),
                this.MakeGridHeader(x => "wtmtemp_CanApprove").SetHide().SetFormat((a, b) =>
                {
                    if(a.wtmtemp_CanApprove) { return "true"; }
                    else { return "false"; }
                }),


                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<OtherOutlay_View> GetSearchQuery()
        {
            var query = DC.Set<OtherOutlay>()

                .CheckContain(Searcher.OtherOutlayCode, x => x.OtherOutlayCode)
                .CheckEqual(Searcher.SupplierId, x => x.SupplierId)
                .CheckEqual(Searcher.MakeUserId, x => x.MakeUserId)
                .CheckBetween(Searcher.InvoicesDate?.GetStartTime(), Searcher.InvoicesDate?.GetEndTime(), x => x.InvoicesDate, includeMax: false)
                .CheckEqual(Searcher.SettlementAccountId, x => x.SettlementAccountId)
                .CheckEqual(Searcher.OutlayTypeId, x => x.OutlayTypeId)
                .CheckEqual(Searcher.TotalPrice, x => x.TotalPrice)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x => x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x => x.UpdateBy)
                .Select(x => new OtherOutlay_View
                {
                    ID = x.ID,

                    OtherOutlay_OtherOutlayCode = x.OtherOutlayCode,
                    OtherOutlay_Supplier = DC.Set<Supplier>().Where(z0 => z0.SupplierCode == x.SupplierId).Select(y => y.SupplierName).FirstOrDefault(),
                    OtherOutlay_MakeUser = DC.Set<FrameworkUser>().Where(z0 => z0.ITCode == x.MakeUserId).Select(y => y.Name).FirstOrDefault(),
                    OtherOutlay_InvoicesDate = x.InvoicesDate,
                    OtherOutlay_SettlementAccount = DC.Set<SettlementAccount>().Where(z0 => z0.Code == x.SettlementAccountId).Select(y => y.Name).FirstOrDefault(),
                    OtherOutlay_OutlayType = x.OutlayType.DicFieldDes,
                    OtherOutlay_TotalPrice = x.TotalPrice,
                    OtherOutlay_Status = x.Status,
                    OtherOutlay_Remark = x.Remark,
                    OtherOutlay_CreateTime = x.CreateTime,
                    OtherOutlay_UpdateTime = x.UpdateTime,
                    OtherOutlay_CreateBy = x.CreateBy,
                    OtherOutlay_UpdateBy = x.UpdateBy,
                    OtherOutlay_IsValid = x.IsValid,
                })
                .OrderBy(x => x.ID);
            return query;
        }


        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null)
            {
                var ids = GetMyApproves();
                foreach (var item in EntityList)
                {
                    var m = ids.Find(x => x.ModelID == item.GetID().ToString());
                    if (m != null)
                    {
                        item.wtmtemp_CanApprove = true;
                    }
                }
            }
        }


    }
    public class OtherOutlay_View : OtherOutlay
    {

        public string OtherOutlay_OtherOutlayCode { get; set; }
        public string OtherOutlay_Supplier { get; set; }
        public string OtherOutlay_MakeUser { get; set; }
        public DateTime? OtherOutlay_InvoicesDate { get; set; }
        public string OtherOutlay_SettlementAccount { get; set; }
        public string OtherOutlay_OutlayType { get; set; }
        public decimal? OtherOutlay_TotalPrice { get; set; }
        public StatusTypeEnum? OtherOutlay_Status { get; set; }
        public string OtherOutlay_Remark { get; set; }
        public DateTime? OtherOutlay_CreateTime { get; set; }
        public DateTime? OtherOutlay_UpdateTime { get; set; }
        public string OtherOutlay_CreateBy { get; set; }
        public string OtherOutlay_UpdateBy { get; set; }
        public bool OtherOutlay_IsValid { get; set; }
        public bool wtmtemp_CanApprove { get; set; }

    }

}